<?php
/*
在程序开始之前检查，是否存在当年的订单表，订单详情表，对账单，操作日志表，IPS通知表
代理商订单表，代理商对账单表，保证金表，物流表，如果没有，直接创建
*/
namespace Behaviors;
class CreateNewTableBehavior{
function run($arg){
//echo "第一个行为".$arg;

$current_year=date("Y");

//订单表
$sql=<<<EOT
CREATE TABLE IF NOT EXISTS `ips_orders_$current_year` (
  `order_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '订单号',
  `ips_id` varchar(50) NOT NULL COMMENT '合作支付号',
  `mh_oid` varchar(50) NOT NULL COMMENT '商户订单号',
  `order_amount` decimal(12,2) NOT NULL COMMENT '订单金额',
  `order_currency` char(3) NOT NULL COMMENT '货币符号',
  `order_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '订单时间',
  `order_status` tinyint(3) unsigned NOT NULL DEFAULT '1' COMMENT '订单状态',
  `order_source_url` varchar(200) NOT NULL COMMENT '订单来源URL',
  `order_source_ip` varchar(50) NOT NULL COMMENT '订单来源IP',
  `order_signature_info` varchar(400) NOT NULL,
  `order_syn_url` varchar(200) NOT NULL COMMENT '同步通知的URL',
  `order_asyn_url` varchar(200) NOT NULL COMMENT '异步通知的URL',
  `order_channel` int(11) NOT NULL COMMENT '订单通道',
  `order_bill_desc` varchar(100) NOT NULL COMMENT '订单账单名称',
  `order_roe` float(12,4) NOT NULL COMMENT '汇率换算费率',
  `base_amount` decimal(12,2) NOT NULL COMMENT '平台基础货币金额',
  `gateway_amount` decimal(12,2) NOT NULL COMMENT '提交到通道的货币金额',
  `gateway_currency` char(3) NOT NULL COMMENT '通道的货币',
  `gateway_roe` float(12,4) NOT NULL COMMENT '通道货币的兑换率',
  `gateway_roe_tpl` float(5,4) NOT NULL COMMENT '汇率差，最小为0，最大0.1',
  `card_type` enum('visa','mastercard','amex','dinersclub','discover','unionpay','jcb') NOT NULL COMMENT '信用卡类型',
  `process_mode` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1消费0预授权',
  `cc_cardid` varchar(200) NOT NULL COMMENT '信用卡ID',
  `cc_cvv` varchar(5) NOT NULL COMMENT 'cvv',
  `cc_expiry_month` varchar(5) NOT NULL COMMENT '过期月',
  `cc_expiry_year` varchar(5) NOT NULL COMMENT '过期年',
  `cc_card_holder` varchar(100) NOT NULL COMMENT '持卡人',
  `bill_email` varchar(50) NOT NULL COMMENT '顾客邮箱',
  `bill_country` char(2) NOT NULL COMMENT '账单国家',
  `bill_state` varchar(50) NOT NULL COMMENT '州省',
  `bill_city` varchar(50) NOT NULL COMMENT '城市',
  `bill_street` varchar(200) NOT NULL COMMENT '详细地址',
  `bill_phone` varchar(20) NOT NULL COMMENT '电话号码',
  `bill_zip` varchar(50) NOT NULL COMMENT '邮编',
  `bill_firstname` varchar(50) NOT NULL COMMENT '顾客账单名',
  `bill_lastname` varchar(50) NOT NULL COMMENT '顾客账单姓',
  `order_mid` int(11) NOT NULL COMMENT '商户号',
  `order_rolling_rate` float(5,4) NOT NULL COMMENT '保证金费率',
  `order_rolling_days` int(11) NOT NULL,
  `order_siteid` int(11) NOT NULL COMMENT '网站号',
  `order_fixedfee` decimal(4,2) NOT NULL DEFAULT '0.00' COMMENT '固定交易费',
  `order_process_rate` float(5,4) NOT NULL COMMENT '交易费率',
  `order_refund_fee` decimal(5,2) NOT NULL COMMENT '退款费',
  `order_declined_fee` decimal(5,2) NOT NULL COMMENT '失败费',
  `order_cb_fee` decimal(5,2) NOT NULL COMMENT '拒付费',
  `order_asyn_status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否发送异步通知',
  `order_asyn_count` tinyint(4) NOT NULL DEFAULT '0' COMMENT '发送次数，默认如果异步接口不返回100，就发送最多三次',
  `order_rebill_id` int(11) NOT NULL DEFAULT '0' COMMENT '周期交易的ID',
  `order_isrisk` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否风险交易',
  `order_risk_desc` varchar(200) NOT NULL COMMENT '风险原因',
  PRIMARY KEY (`order_id`),
  KEY `order_channel` (`order_channel`),
  KEY `order_mid` (`order_mid`),
  KEY `order_siteid` (`order_siteid`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8  ;
EOT;

//订单详情表
$sql.=<<<EOT
CREATE TABLE IF NOT EXISTS `ips_orderhistory_$current_year` (
  `oh_id` int(11) NOT NULL AUTO_INCREMENT,
  `oh_oid` int(11) NOT NULL,
  `oh_status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '变动状态',
  `oh_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `oh_amount` decimal(12,2) NOT NULL,
  `oh_origin_amount` decimal(12,2) NOT NULL DEFAULT '0.00' COMMENT '订单原始货币计价金额',
  `oh_gateway_amount` decimal(12,2) NOT NULL DEFAULT '0.00' COMMENT '订单对应通道金额',
  `oh_remark` varchar(500) NOT NULL,
  PRIMARY KEY (`oh_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='订单历史信息表';
EOT;

//对账单表
$sql.=<<<EOT
CREATE TABLE IF NOT EXISTS `ips_statement_$current_year` (
  `st_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '流水号',
  `st_mid` int(11) NOT NULL COMMENT '商户id',
  `st_flag` tinyint(1) NOT NULL DEFAULT '1' COMMENT '标志 1表示增加 0表示减少',
  `st_name` tinyint(4) NOT NULL COMMENT '对账名',
  `st_amount` decimal(12,2) NOT NULL COMMENT '(账户变动)金额',
  `st_fee` decimal(12,2) NOT NULL COMMENT '费用',
  `st_reason` varchar(400) NOT NULL COMMENT '原因',
  `st_balance` decimal(12,2) NOT NULL COMMENT '余额',
  `st_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '对账时间',
  PRIMARY KEY (`st_id`),
  KEY `st_mid` (`st_mid`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
EOT;

//操作日志表
$sql.=<<<EOT
CREATE TABLE IF NOT EXISTS `ips_weblog_$current_year` (
  `log_id` int(11) NOT NULL AUTO_INCREMENT,
  `log_admin` varchar(100) NOT NULL,
  `log_action` varchar(100) NOT NULL,
  `log_sql` varchar(200) NOT NULL,
  `log_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`log_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='操作日志';
EOT;

//ips通知表
$sql.=<<<EOT
CREATE TABLE IF NOT EXISTS `ips_innotice_$current_year` (
  `in_id` int(11) NOT NULL AUTO_INCREMENT,
  `in_oid` int(11) NOT NULL COMMENT '订单ID',
  `in_title` varchar(200) NOT NULL,
  `in_content` text NOT NULL,
  `in_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`in_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='内部通知，一般来自IPS系统的通知' ;
EOT;

//代理商订单表
$sql.=<<<EOT
CREATE TABLE IF NOT EXISTS `ips_affiliate_order_$current_year` (
  `aff_id` int(11) NOT NULL,
  `order_id` int(11) NOT NULL,
  `aff_rate` float(5,4) NOT NULL,
  `aff_commission` decimal(12,2) NOT NULL,
  `aff_refund_fee` decimal(12,2) NOT NULL DEFAULT '0.00',
  `aff_cb_fee` decimal(12,2) NOT NULL DEFAULT '0.00',
  `aff_failed_fee` decimal(12,2) NOT NULL DEFAULT '0.00',
  `aff_fixed_fee` decimal(12,2) NOT NULL DEFAULT '0.00' COMMENT '固定交易费',
  PRIMARY KEY (`aff_id`,`order_id`),
  KEY `order_id` (`order_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='代理商交易对应表';
EOT;

//代理商对账单表
$sql.=<<<EOT
CREATE TABLE IF NOT EXISTS `ips_affrecord_$current_year` (
  `ar_id` int(11) NOT NULL AUTO_INCREMENT,
  `ar_aff_id` int(11) NOT NULL,
  `ar_amount` decimal(12,2) NOT NULL DEFAULT '0.00',
  `ar_status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '变动标志（增加或是减少）',
  `ar_type` tinyint(4) NOT NULL,
  `ar_balance` decimal(12,2) NOT NULL DEFAULT '0.00' COMMENT '余额',
  `ar_fee` decimal(12,2) NOT NULL DEFAULT '0.00',
  `ar_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `ar_remark` varchar(600) NOT NULL COMMENT ' 备注',
  PRIMARY KEY (`ar_id`),
  KEY `ar_aff_id` (`ar_aff_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='代理商对账记录表' ;
EOT;

//保证金表
$sql.=<<<EOT
CREATE TABLE IF NOT EXISTS `ips_rolling_$current_year` (
  `rl_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '流水号',
  `rl_mid` int(11) NOT NULL COMMENT '商户号',
  `rl_amount` decimal(12,2) NOT NULL COMMENT '金额',
  `rl_status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '状态',
  `rl_oid` int(11) NOT NULL COMMENT '订单号',
  `rl_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '日期',
  `rl_enddate` datetime NOT NULL COMMENT '返回日期',
  `rl_remark` varchar(400) NOT NULL COMMENT '备注',
  PRIMARY KEY (`rl_id`),
  KEY `rl_mid` (`rl_mid`),
  KEY `rl_oid` (`rl_oid`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;
EOT;

//物流表
$sql.=<<<EOT
CREATE TABLE IF NOT EXISTS `ips_delivery_$current_year` (
  `dv_id` int(11) NOT NULL AUTO_INCREMENT,
  `dv_oid` int(11) NOT NULL,
  `dv_tk_id` varchar(100) NOT NULL,
  `dv_number` varchar(200) NOT NULL,
  `dv_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `dv_isflag` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否标记为问题运单',
  `dv_remark` varchar(400) NOT NULL,
  PRIMARY KEY (`dv_id`),
  UNIQUE KEY `dv_oid` (`dv_oid`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='物流表' ;
EOT;

/*
$sql=<<<EOT
drop table `ips_orders_$current_year`;
drop table `ips_orderhistory_$current_year`;
drop table `ips_statement_$current_year` ;
drop table `ips_weblog_$current_year` ;
drop table `ips_innotice_$current_year`;
drop table `ips_affiliate_order_$current_year`;
drop table `ips_affrecord_$current_year`;
drop table `ips_rolling_$current_year`;
drop table  `ips_delivery_$current_year`;
EOT;
*/

$oop=M();
$oop->execute($sql);

}
}